package com.smartHome.service.impl;

import com.smartHome.mapper.DeviceMapper;
import com.smartHome.pojo.entity.Device;
import com.smartHome.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public void add(Device device) {
        if(device.getDeviceName().equals("空调")){
            device.setNumerical(22);
            device.setPattern("冷风");
        }else if(device.getDeviceName().equals("智能灯")){
            device.setNumerical(50);
            device.setPattern("冷暖色");
        }else if(device.getDeviceName().equals("加湿器")){
            device.setNumerical(50);
            device.setPattern("恒湿");
        }
        else if(device.getDeviceName().equals("净化器")){
            device.setNumerical(0);
            device.setPattern("自动");
        }else{
            device.setNumerical(0);
            device.setPattern("中速");
        }
        deviceMapper.addDevice(device);
    }

    @Override
    public void delete(Integer deviceId) {
        deviceMapper.delete(deviceId);
    }

    /**
     * 通过房间id查找所有设备
     * 
     * @param roomId
     * @return
     */
    @Override
    public List<Device> getDeviceList(Integer roomId) {
        return deviceMapper.getDeviceList(roomId);
    }

    /**
     * 修改设备的状态
     * 
     * @param device
     */
    @Override
    public void update(Device device) {
        deviceMapper.update(device);
    }

    /**
     * 根据设备Id获取设备详细信息
     * 
     * @return
     */
    @Override
    public Device getDevice(Integer deviceId) {
        return deviceMapper.getDevice(deviceId);
    }

    @Override
    public void turnOffAllDevices(Integer roomId) {
        deviceMapper.turnOffAllDevices(roomId);
    }

    @Override
    public List<Device> getAllDevices() {
        return deviceMapper.getAllDevices();
    }

    @Override
    public BigDecimal getDevicePower(Integer deviceId) {
        return deviceMapper.getDevicePower(deviceId);
    }

    @Override
    @Transactional
    public void turnOffDevicesByHouseId(Integer houseId) {
        deviceMapper.updateStatusByHouseId(houseId, 0);
    }
}
